package com.heima.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.autoconfig.template.MinIOTemplate;
import com.heima.common.thread.MyThreadLocal;
import com.heima.mapper.WmMaterialMapper;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.media.dto.WmMaterialDto;
import com.heima.model.media.pojos.WmMaterial;
import com.heima.service.WmMaterialService;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.UUID;

@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial> implements WmMaterialService {

    @Autowired
    private MinIOTemplate minIOTemplate;

    // 文件上传
    @SneakyThrows
    @Override
    public ResponseResult uploadPicture(MultipartFile file) {
        // 检查参数
        if (file == null || file.getSize() == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 获取文件后缀
        String originalFilename = file.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        // 定义文件名
        String uuid = UUID.randomUUID().toString().replace("-", "");
        String fileName = uuid + suffix;
        // 将图片上传到minio
        String url = minIOTemplate.uploadImgFile("", fileName, file.getInputStream());
        // 将URL写入素材表
        // 定义素材对象
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setCreatedTime(new Date());
        wmMaterial.setUrl(url);
        wmMaterial.setUserId(Integer.parseInt(MyThreadLocal.get()));
        wmMaterial.setType(((short) 0));
        wmMaterial.setIsCollection(((short) 0));

        this.save(wmMaterial);
        // 返回数据
        return ResponseResult.okResult(wmMaterial);
    }

    // 素材列表查询
    @Override
    public PageResponseResult findAll(WmMaterialDto dto) {
        // 检查参数
        Integer currentPage = dto.getPage();
        Integer size = dto.getSize();
        if (currentPage == null || size == null) {
            return null;
        }
        // 定义分页对象
        Page<WmMaterial> page = new Page<>(dto.getPage(), dto.getSize());
        // 定义查询条件
        LambdaQueryWrapper<WmMaterial> wrapper = Wrappers.lambdaQuery(WmMaterial.class)
                //是否收藏
                .eq(dto.getIsCollection() != null, WmMaterial::getIsCollection, dto.getIsCollection())
                //按照用户查询
                .eq(WmMaterial::getUserId, MyThreadLocal.get())
                //按照时间倒序
                .orderByDesc(WmMaterial::getCreatedTime);
        // 分页查询
        Page<WmMaterial> pages = this.page(page, wrapper);
        // 封装数据
        PageResponseResult pageResponseResult = new PageResponseResult(currentPage,size, (int) pages.getTotal());
        pageResponseResult.setData(pages.getRecords());
        // 返回
        return pageResponseResult;
    }
}
